package sort;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void main(String[] args) {
        Student[] s=new Student[5];
        s[0]=new Student("Alice", 18, 1.7);
        s[1]=new Student("Bob", 17, 1.6);
        s[2]=new Student("Charlie", 16, 1.5);
        s[3]=new Student("David", 19, 1.8);
        s[4]=new Student("Eve", 18, 1.7);
        Arrays.sort(s, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // 先比较年龄
                if (s1.getAge() != s2.getAge()) {
                    return Integer.compare(s1.getAge(), s2.getAge());
                }
                // 年龄相同则比较身高
                return Double.compare(s1.getHeight(), s2.getHeight());
//                if(s1.getAge() != s2.getAge()){
//                    return s1.getAge-s2.getAge();
//                }else{
//                    return (int) (s1.getHeight()-s2.getHeight());
//                }
            }
        });
        // 打印排序结果
        System.out.println("排序后的学生列表：");
        for (Student student : s) {
            System.out.println(student);
        }
    }
}
